﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 路线推荐
    /// </summary>
    /// Author:蒲崇彬
    /// Creation date:2015-8-3 11:42
    public class CBizAdditionalInfo : CustBizObject
    {
        private CDmDjkAdditionalInfo m_CDmDjkAdditionalInfo = new CDmDjkAdditionalInfo();
        private CDmDjkAdditionalInfo oDmDjkAdditionalInfo
        {
            get { return m_CDmDjkAdditionalInfo; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizAdditionalInfo()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkAdditionalInfo.DJK_ADDITIONAL_INFO);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ADDITIONAL_INFO_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }
        /// <summary>
        /// 添加 推荐的地接客
        /// </summary>
        /// <param name="djkList">地接客id</param>
        /// <param name="routeID">路线</param>
        /// <returns>string</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-8-3 13:45
        public string AddItionalInfo(List<string> djkList, string routeID)
        {

            if (djkList.Count > 10)
            {
                return "地接客推荐个数不能超过10个";
            }

            DataSet dsDjkInfo = new CDmDjkAdditionalInfo();

            foreach (string item in djkList)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    DataRow drInfo = dsDjkInfo.Tables[0].NewRow();
                    drInfo["ADDITIONAL_INFO_ID"] = this.DataObj.NewID();
                    drInfo["DJK_USERID"] = item;
                    drInfo["ROUTE_ID"] = routeID;
                    dsDjkInfo.Tables[0].Rows.Add(drInfo);
                }
            }
            dsDjkInfo.Tables[0].TableName = this.TableName;
            this.Update(dsDjkInfo);

            return "OK";
        }

        /// <summary>
        /// 修改推荐的地接客
        /// </summary>
        /// <param name="djkList">地接客id</param>
        /// <param name="routeID">路线</param>
        /// <returns>string</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-8-3 13:45
        public string UpdateItionalInfo(List<string> djkList, string routeID)
        {

            if (djkList.Count > 5)
            {
                return "地接客推荐个数不能超过5个";
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("routeID", ParameterDirection.Input, EDbType.String, 36, routeID));
            //删除
            int exCount = this.DataObj.ExecuteBySQL(@"DELETE [DJK_ADDITIONAL_INFO] WHERE ROUTE_ID=@routeID", parmCollection);

            if (djkList.Count < 1)
            {
                return "OK";
            }
            //添加
            DataSet dsDjkInfo = new CDmDjkAdditionalInfo();
            foreach (string item in djkList)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    DataRow drInfo = dsDjkInfo.Tables[0].NewRow();
                    drInfo["ADDITIONAL_INFO_ID"] = this.DataObj.NewID();
                    drInfo["DJK_USERID"] = item;
                    drInfo["ROUTE_ID"] = routeID;
                    dsDjkInfo.Tables[0].Rows.Add(drInfo);
                }
            }
            dsDjkInfo.Tables[0].TableName = this.TableName;
            this.Update(dsDjkInfo);

            return "OK";
        }



        /// <summary>
        /// 根据 线路查询推荐 的地接客信息
        /// </summary>
        /// <param name="routeID">推荐线路iD</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-8-3 14:37
        public DataTable GetItionalInfolList(string routeID)
        {
            if (string.IsNullOrEmpty(routeID))
            {
                return null;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("routeID", ParameterDirection.Input, EDbType.String, 36, routeID));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT DA.*,DC.CUSTOMER_NICK,DC.CUSTOMER_SOURCE_PIC FROM [dbo].[DJK_ADDITIONAL_INFO](NOLOCK) DA
LEFT JOIN 
 [dbo].[DJK_CUSTOMER_REGISTER](NOLOCK) DC
ON DA.DJK_USERID=DC.CUSTOMER_REGISTER_ID WHERE DA.ROUTE_ID=@routeID", parmCollection);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            return ds.Tables[0];
        }

    }
}
